﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DPhoneUser
    {
        
        public bool Exists(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tphoneuser");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.String)
};
            parameters[0].Value = phoneNumber;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }
         
        public  bool Add(Model.PhoneUserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tphoneuser(");
            strSql.Append("PhoneNumber,IMEI,RegistTime,LastLoginTime,");
            strSql.Append("LoginCount,Name)");
            strSql.Append(" values (");
            strSql.Append("@PhoneNumber,@IMEI,@RegistTime,@LastLoginTime,");
            strSql.Append("@LoginCount,@Name)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),
					new MySqlParameter("@RegistTime", MySqlDbType.DateTime),
                    new MySqlParameter("@LastLoginTime", MySqlDbType.DateTime),				 
                    new MySqlParameter("@LoginCount", MySqlDbType.VarChar,15),
                    new MySqlParameter("@Name", MySqlDbType.VarChar,10)};
            parameters[0].Value = model.PhoneNumber;
            parameters[1].Value = model.Imei;
            parameters[2].Value = model.RegistTime;
            parameters[3].Value = model.LastLoginTime;
            parameters[4].Value = 1;
            parameters[5].Value = model.Name;
                          
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public  bool ClearImei(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tphoneuser set "); 
            strSql.Append("IMEI='' ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15),
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),				 
                    new MySqlParameter("@Id", MySqlDbType.Int32)};
            parameters[0].Value = phoneNumber;
                                 
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateWhenLogin(Model.PhoneUserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tphoneuser set ");            
            strSql.Append("IMEI=@IMEI,");
            strSql.Append("LastLoginTime=@LastLoginTime,");
            strSql.Append("LoginCount = LoginCount+1 ");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {				
					new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),
					new MySqlParameter("@LastLoginTime", MySqlDbType.DateTime),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            parameters[0].Value = model.Imei;
            parameters[1].Value = model.LastLoginTime;
            parameters[2].Value = model.PhoneNumber;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        } 

        public bool UpdateInfoByUser(string strPhone,string strName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tphoneuser set ");
            strSql.Append("Name=@Name ");             
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {				
					new MySqlParameter("@Name", MySqlDbType.VarChar,10),					
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,15)};
            parameters[0].Value = strName;
            parameters[1].Value = strPhone;
             
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tphoneuser ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
       

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Model.PhoneUserInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.id,t.PhoneNumber,t.IMEI,t.RegistTime,");
            strSql.Append("t.LastLoginTime,t.LoginCount");
            strSql.Append(" from tphoneuser t  "); 
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
            parameters[0].Value = id;

            Model.PhoneUserInfo model = new Model.PhoneUserInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IMEI"] != null && ds.Tables[0].Rows[0]["IMEI"].ToString() != "")
                {
                    model.Imei = ds.Tables[0].Rows[0]["IMEI"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RegistTime"] != null && ds.Tables[0].Rows[0]["RegistTime"].ToString() != "")
                {
                    model.RegistTime = DateTime.Parse(ds.Tables[0].Rows[0]["RegistTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastLoginTime"] != null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
                {
                    model.LastLoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LoginCount"] != null && ds.Tables[0].Rows[0]["LoginCount"].ToString() != "")
                {
                    model.LoginCount = int.Parse(ds.Tables[0].Rows[0]["LoginCount"].ToString());
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.PhoneUserInfo GetModelByNumber(string phoneNumber)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.id,t.PhoneNumber,t.IMEI,t.RegistTime,");
            strSql.Append("t.LastLoginTime,t.LoginCount,t.Name ");
            strSql.Append(" from tphoneuser t  "); 
            strSql.Append(" where t.PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar)};
            parameters[0].Value = phoneNumber;

            Model.PhoneUserInfo model = new Model.PhoneUserInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IMEI"] != null && ds.Tables[0].Rows[0]["IMEI"].ToString() != "")
                {
                    model.Imei = ds.Tables[0].Rows[0]["IMEI"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RegistTime"] != null && ds.Tables[0].Rows[0]["RegistTime"].ToString() != "")
                {
                    model.RegistTime = DateTime.Parse(ds.Tables[0].Rows[0]["RegistTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastLoginTime"] != null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
                {
                    model.LastLoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LoginCount"] != null && ds.Tables[0].Rows[0]["LoginCount"].ToString() != "")
                {
                    model.LoginCount = int.Parse(ds.Tables[0].Rows[0]["LoginCount"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tphoneuser t ");
        
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
         
    }
}
